package com.example.administrator.liangcang;

import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.administrator.liangcang.databinding.ActivityMainBinding;
import com.example.administrator.liangcang.fragment.DarenFragment;
import com.example.administrator.liangcang.fragment.GoodFragment;
import com.example.administrator.liangcang.fragment.MessageFragment;
import com.example.administrator.liangcang.fragment.MySelfFragment;
import com.example.administrator.liangcang.fragment.StoreFragment;
import com.example.administrator.liangcang.store.fragment.ThemeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private List<Fragment> fragments;
    private boolean isExit = false;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        aboutFragments();
        aboutFragmentsTransaction();
        aboutGroupListener();

    }

    private void aboutGroupListener() {
        binding.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.button0:
                        getSupportFragmentManager().beginTransaction().show(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).hide(fragments.get(4)).commit();
                        break;
                    case R.id.button1:
                        getSupportFragmentManager().beginTransaction().show(fragments.get(1)).hide(fragments.get(0)).hide(fragments.get(2)).hide(fragments.get(3)).hide(fragments.get(4)).commit();
                        break;
                    case R.id.button2:
                        getSupportFragmentManager().beginTransaction().show(fragments.get(2)).hide(fragments.get(1)).hide(fragments.get(0)).hide(fragments.get(3)).hide(fragments.get(4)).commit();
                        break;
                    case R.id.button3:
                        getSupportFragmentManager().beginTransaction().show(fragments.get(3)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(0)).hide(fragments.get(4)).commit();
                        break;
                    case R.id.button4:
                        getSupportFragmentManager().beginTransaction().show(fragments.get(4)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).hide(fragments.get(0)).commit();
                        break;
                }
            }
        });
    }

    private void aboutFragmentsTransaction() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_container, fragments.get(0))
                .add(R.id.fragment_container,fragments.get(1))
                .add(R.id.fragment_container,fragments.get(2))
                .add(R.id.fragment_container,fragments.get(3))
                .add(R.id.fragment_container,fragments.get(4))
                .show(fragments.get(0))
        .hide(fragments.get(2))
        .hide(fragments.get(1))
        .hide(fragments.get(3))
        .hide(fragments.get(4));
        transaction.commit();
    }

    private void aboutFragments() {
        fragments = new ArrayList<>();
        fragments.add(new StoreFragment());
        fragments.add(new MessageFragment());
        fragments.add(new DarenFragment());
        fragments.add(new GoodFragment());
        fragments.add(new MySelfFragment());
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
            exit();
            return  false ;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void exit(){
        if(!isExit){
            isExit = true;
            Toast.makeText(MainActivity.this, "再按一次退出良仓", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(0,2000);
        }else{
            finish();
            System.exit(0);
        }
    }
}
